package org.niu.dfs;

import java.lang.reflect.Array;
import java.util.Arrays;

public class NumberOfIsolation {
    public int solve (char[][] grid) {
        // write code here
        int m = grid.length;
        int n = grid[0].length;
        boolean[][] visited = new boolean[m][n];
        int ans = 0;
        for (int i = 0;i < m;i++){
            for (int j = 0; j < n; j++) {
                if(!visited[i][j] && grid[i][j]=='1'){
                    ans++;
                    dfs(grid,visited,i,j);
                }
            }
        }
        return ans;
    }
    void dfs(char[][] grid,boolean[][] v,int i,int j){
        if(i < 0||i >= grid.length||j < 0|| j >= grid[0].length||v[i][j]||grid[i][j]=='0'){
            return;
        }
        grid[i][j]=0;
        v[i][j]=true;
        dfs(grid,v,i+1,j);
        dfs(grid,v,i-1,j);
        dfs(grid,v,i,j+1);
        dfs(grid,v,i,j-1);
    }
}
